<!DOCTYPE html>
<html lang="zh">

<head>
	<meta charset="UTF-8">
	<title>管理员 - 操作</title>
	<meta name="referrer" content="never">
	<link rel="stylesheet" href="../assets/lib/layui/css/layui.css" media="all">
	<link rel="stylesheet" href="../assets/css/public.css" media="all">
</head>

<body>
	<div class="iframe-container">
		<div class="iframe-main">
			<form class="layui-form" lay-filter="admin_action">
				<input type="number" name="id" lay-verify="int" autocomplete="off" class="layui-input layui-hide">
				<div class="layui-form-item">
					<label class="layui-form-label required">用户名</label>
					<div class="layui-input-block">
						<input type="text" name="user" lay-verify="required" maxlength="30" lay-reqtext="用户名是必填项，不能为空！" placeholder="请输入用户名" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label required">密码</label>
					<div class="layui-input-block">
						<input type="text" name="pass" lay-verify="required" maxlength="30" lay-reqtext="密码是必填项，不能为空！" placeholder="请输入密码" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">姓名</label>
					<div class="layui-input-block">
						<input type="text" name="name" lay-verify="zh" maxlength="4" placeholder="请输入" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">性别</label>
					<div class="layui-input-block">
						<input type="radio" name="sex" value="1" title="男" checked>
						<input type="radio" name="sex" value="0" title="女">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">生日</label>
					<div class="layui-input-block">
						<input type="text" name="birthday" lay-verify="date" autocomplete="off" class="layui-input" readonly>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">手机号码</label>
					<div class="layui-input-block">
						<input type="text" name="mobile" maxlength="11" lay-verify="mobile" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">邮箱</label>
					<div class="layui-input-block">
						<input type="text" name="email" maxlength="50" lay-verify="email2" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">头像</label>
					<div class="layui-input-block">
						<input type="text" name="avatar" maxlength="255" autocomplete="off" class="layui-input layui-hide">
						<div class="layui-upload-drag" id="avatar-upload">
							<i class="layui-icon layui-icon-upload"></i>
							<img src="" alt="头像" width="50px" height="50px" class="layui-hide">
							<p>点击上传，或将文件拖拽到此处</p>
						</div>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">状态</label>
					<div class="layui-input-block">
						<input type="checkbox" name="status" lay-skin="switch" lay-filter="status" lay-text="启用|禁用" checked>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button type="submit" class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
					</div>
				</div>
			</form>
		</div>
	</div>
	<script src="../assets/lib/layui/layui.js" charset="utf-8"></script>
	<script src="../assets/js/lay-config.js" charset="utf-8"></script>
	<script src="../assets/js/public.min.js" charset="utf-8"></script>
	<script type="text/javascript">
		if (!window.frameElement) location.href = '/';

		layui.use(['jquery', 'form', 'laydate', 'upload'], function() {
			var $ = layui.jquery,
				form = layui.form,
				laydate = layui.laydate,
				upload = layui.upload;

			laydate.render({
				elem: $('.layui-form input[name="birthday"]')[0],
				type: 'date',
				trigger: 'click'
			});

			var params = parent.params || {};

			var loading;
			var uploadIns = upload.render({
				elem: '#avatar-upload',
				url: lianyi.api({ m: 'imgUpload' }, 1),
				accept: 'images',
				acceptMime: 'images',
				exts: 'gif|jpg|jpeg|png',
				size: 1024 * 10,
				before: function(obj) {
					loading = lianyi.load('上传中...');
				},
				done: function(res) {
					var code = res.code,
						msg = res.msg;

					if (res.code === 0)
						lianyi.success(msg, function() {
							$('#avatar-upload > .layui-icon-upload').addClass('layui-hide');
							$('#avatar-upload > img').removeClass('layui-hide').attr('src', res.imgUrl);
							form.val('admin_action', { avatar: res.imgUrl });
						});
					else
						lianyi.fail(msg, function() {
							lianyi.jump(res);
						});
				},
				error: function(index, upload) {
					layer.close(loading);
					lianyi.fail('上传出错');
				}
			});

			if (params.data) {
				if (params.data.avatar) {
					$('#avatar-upload > .layui-icon-upload').addClass('layui-hide');
					$('#avatar-upload > img').removeClass('layui-hide').attr('src', params.data.avatar);
				}
				form.val('admin_action', {
					id: params.data.id,
					pId: params.data.pId,
					user: params.data.user,
					pass: params.data.pass,
					name: params.data.name,
					sex: params.data.sex,
					birthday: params.data.birthday,
					mobile: params.data.mobile,
					email: params.data.email,
					avatar: params.data.avatar,
					loginTime: params.data.loginTime,
					status: params.data.status === 1
				});
			} else
				form.val('admin_action', { status: true });

			form.on('submit(submit)', function(data) {
				var field = data.field;
				if (field.hasOwnProperty('file')) delete field.file;
				field.status = field.status === 'on' ? 1 : 0;

				var c = 'update';
				if (!/^\s*(0|[1-9]\d*)\s*$/.test(field.id)) {
					delete field.id;
					c = 'add';
				} else if (false === (field = updateParams(field, params.data))) {
					lianyi.fail('无变化！');
					return false;
				}

				lianyi.request({
					url: lianyi.api(c, 1),
					data: field,
					msg: '正在' + (c === 'add' ? '添加' : '修改') + '...',
					success: function(res) {
						if (params.data) parent.upTable(field);
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);
					}
				});

				return false;
			});
		});
	</script>
</body>

</html>